const dao = require('./base');

module.exports = {

    channelList: async function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from afc_channel where del_flag=0`;
        let pre = [];
        if (condition.channel !== undefined && condition.channel !== '') {
            sql += ` and channel like %?%`;
            pre.push(condition.channel);
        }
        sql = dao.by(sql, by)

        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;

        let data;
        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },

    channelAdd: async function (fields = []) {
        let sql = "insert into afc_channel (channel) values(?)";
        let pre = [];
        let data;
        pre.push(fields.channel);
        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },

    channelUpdate: async function (condition = {}, fields = {}) {
        let sql = "update afc_channel set ";
        let pre = [];
        let data;
        if (fields.channel !== undefined && fields.channel !== '') {
            sql = dao.update_sql_splice(sql, "channel");
            pre.push(fields.channel);
        }
        if (fields.del_flag !== undefined && fields.del_flag !== '') {
            sql = dao.update_sql_splice(sql, "del_flag");
            pre.push(fields.del_flag);
        }
        if (condition.cid !== undefined && condition.cid !== '') {
            sql += ` where cid = ?`;
            pre.push(condition.cid);
        } else {
            return;
        }

        await dao.query(sql, pre).then((results, fields) => {
            data = results;
        }).catch((err) => {
            data = {err: err}
        });
        return data;
    },


};
